package com.pro.controller;

import com.pro.domain.Seat;
import com.pro.service.SeatService;
import com.pro.util.Division;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class SeatController {
    @Autowired(required = false)
    private SeatService seatService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /*进入seat页面时通过timeid查已被选的座位集合*/
    @CrossOrigin
    @RequestMapping("/selectPosition")
    public List selectPosition(int timeid){
        List positionList = seatService.selectPosition(timeid);
        return  positionList;
    }

    /*增加*/
    /*将选中的position，memberid，timeid存入表seat中*/
    @CrossOrigin
    @RequestMapping("/insertPosition")
    public void insertPosition(int timeid,int memberid,String position){
        seatService.insertPosition(timeid, memberid, position);
    }

    /*删除*/
    /*根据memberid，timeid先查出刚刚有没有选，如果有就删除该条记录*/
    @CrossOrigin
    @RequestMapping("/deletePosition")
    public void deletePosition(int timeid,int memberid,String position){
        seatService.deletePosition(timeid, memberid,position);
    }

    /*根据memberid和timeid查已登录的该用户所选的座位*/
    @CrossOrigin
    @RequestMapping("/selectPositionBymemberid")
    public List selectPosition(int timeid,int memberid){
        List positionList = seatService.selectPositionBymemberid(timeid,memberid);
        return  positionList;
    }

    /*根据positionList,一个个取出来，执行工具类Division*/
    @CrossOrigin
    @RequestMapping("/getDivision")
    public Map getDivision(String timeid,String memberid){
        int timeid1 = Integer.parseInt(timeid);
        int memberid1 = Integer.parseInt(memberid);
        Division division = new Division();
        Map map = new HashMap();
        List hangshuList = new ArrayList();
        List lieshuList = new ArrayList();
        /*先拿到该用户自己刚刚选择的positionList*/
        List positionList = seatService.selectPositionBymemberid(timeid1,memberid1);
        for (int i = 0; i < positionList.size(); i++) {
             String position = (String) positionList.get(i);
             int position1 =Integer.parseInt(position);
            int hangshu = division.Hangshu(position1);
            int lieshu = division.Lieshu(position1);
            hangshuList.add(hangshu);
            lieshuList.add(lieshu);
        }
        map.put("hangshu",hangshuList);
        map.put("lieshu",lieshuList);
        return map;
    }
}
